package com.lg.partition60;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/26 16:16
 */
public class P5960模板_差分约束算法 {

    private static final int N = 5010, M = N, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[] dist = new int[N], q = new int[N], cnt = new int[N];
    private static boolean[] inq = new boolean[N];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt(); m = nextInt();
        Arrays.fill(h, -1);
        for (int i = 0; i < m; i++) {
            int c1 = nextInt(), c2 = nextInt(), y = nextInt();
            add(c2, c1, y);
        }
        if (spfa()) {
            for (int i = 1; i <= n; i++) writer.format("%d ", dist[i]);
        } else {
            writer.println("NO");
        }
        writer.flush();
    }

    private static boolean spfa() {
        int qh = 0, qt = 0;
        for (int i = 1; i <= n; i++) {
            q[qt++] = i;
            inq[i] = true;
            dist[i] = INF;
            cnt[i]++;
        }
        dist[1] = 0;
        while (qh != qt) {
            int u = q[qh++];
            if (qh == N) qh = 0;
            inq[u] = false;
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (dist[v] > dist[u] + w[i]) {
                    dist[v] = dist[u] + w[i];
                    cnt[v] = cnt[u] + 1;
                    if (cnt[v] > n) return false;
                    if (inq[v]) continue;
                    q[qt++] = v;
                    if (qt == N) qt = 0;
                    inq[v] = true;
                }
            }
        }
        return true;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
